package com.system.Service.Impl;

import com.system.Service.EmailService;
import com.system.Service.SmsService;
import com.system.Service.VerificationCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

// 验证码服务实现
@Service
@Slf4j
public class VerificationCodeServiceImpl implements VerificationCodeService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private EmailService emailService;

    @Autowired
    private SmsService smsService;

    private static final String EMAIL_CODE_PREFIX = "verification:email:";
    private static final String PHONE_CODE_PREFIX = "verification:phone:";
    private static final long CODE_EXPIRE_MINUTES = 3;
    private static final int CODE_LENGTH = 6;

    @Override
    public boolean sendEmailCode(String email) {
        String code = generateVerificationCode();
        String subject = "京剧苑 - 验证码";
        String content = String.format(
                "您好，\n\n您的验证码是：%s\n\n验证码有效期为%d分钟，请勿泄露给他人。\n\n京剧苑团队",
                code, CODE_EXPIRE_MINUTES
        );

        try {
            emailService.sendSimpleMail(email, subject, content);
            redisTemplate.opsForValue().set(
                    EMAIL_CODE_PREFIX + email,
                    code,
                    CODE_EXPIRE_MINUTES,
                    TimeUnit.MINUTES
            );
            return true;
        } catch (Exception e) {
            log.error("邮件发送失败", e);
            return false;
        }
    }

    @Override
    public boolean sendPhoneCode(String phone) {
        String code = generateVerificationCode();
        try {
            boolean sendResult = smsService.sendVerificationCode(phone, code);
            if (sendResult) {
                redisTemplate.opsForValue().set(
                        PHONE_CODE_PREFIX + phone,
                        code,
                        CODE_EXPIRE_MINUTES,
                        TimeUnit.MINUTES
                );
                return true;
            }
            return false;
        } catch (Exception e) {
            log.error("短信发送失败", e);
            return false;
        }
    }

    @Override
    public boolean verifyCode(String type, String receiver, String code) {
        String key = ("email".equals(type) ? EMAIL_CODE_PREFIX : PHONE_CODE_PREFIX) + receiver;
        String savedCode = redisTemplate.opsForValue().get(key);

        if (savedCode != null && savedCode.equals(code)) {
//            redisTemplate.delete(key);
            return true;
        }
        return false;
    }

    private String generateVerificationCode() {
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < CODE_LENGTH; i++) {
            code.append(random.nextInt(10));
        }
        return code.toString();
    }
}

